package controllers;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.Query;

import models.Rating;


public class RatingConnect {
	boolean hasEntries;
	private int iROWS;
	
	public RatingConnect(){
		hasEntries = false;
		iROWS = -1;
	}
	public boolean hasEntries(){
		return hasEntries;
	}
	
	public List<HashMap<String,Object>> getCompleteData(String... fieldNames) {
		List<HashMap<String, Object>> objList = new LinkedList<HashMap<String,Object>>();
		Objectify ofy = ObjectifyService.begin();
		iROWS = -1;
		
    	try {
    		Query<Rating> q = ofy.query(Rating.class);             
    		for ( Rating  f : q.fetch() ) {
    			HashMap<String,Object> itemMap = new HashMap<String,Object>();
    			for ( String fields : fieldNames) 
    			{
    				if ( fields.equalsIgnoreCase("rateID") )   					{   itemMap.put("rateID", f.getRateID()); hasEntries = true; }
    				if ( fields.equalsIgnoreCase("solID") )   					{   itemMap.put("solID", f.getSolID()); hasEntries = true; }
    				if ( fields.equalsIgnoreCase("nominatedON") )				{   itemMap.put("nominatedON", f.getNominatedON()); hasEntries = true; }
    				if ( fields.equalsIgnoreCase("nominateSolutionYear") )		{	itemMap.put("nominateSolutionYear", f.getNominateSolutionYear()); hasEntries = true; }
    				if ( fields.equalsIgnoreCase("nominateSolutionMonth") )   	{   itemMap.put("nominateSolutionMonth", f.getNominateSolutionMonth()); hasEntries = true; }
    				if ( fields.equalsIgnoreCase("bestOfRegionON") )   			{   itemMap.put("bestOfRegionON", f.getBestOfRegionON()); hasEntries = true; }
    				if ( fields.equalsIgnoreCase("bestOfRegionYear") )			{   itemMap.put("bestOfRegionYear", f.getBestOfRegionYear()); hasEntries = true; }
    				if ( fields.equalsIgnoreCase("bestOfRegionMonth") )			{	itemMap.put("bestOfRegionMonth", f.getBestOfRegionMonth()); hasEntries = true; }
    				if ( fields.equalsIgnoreCase("bestOfBestON") )				{	itemMap.put("bestOfBestON", f.getBestOfBestON()); hasEntries = true; }    				
    				if ( fields.equalsIgnoreCase("bestOfBestYear") )			{	itemMap.put("bestOfBestYear", f.getBestOfBestYear()); hasEntries = true; }    				
    				if ( fields.equalsIgnoreCase("bestOfBestMonth") )			{	itemMap.put("bestOfBestMonth", f.getBestOfBestMonth()); hasEntries = true; }
    				if ( fields.equalsIgnoreCase("submittedON") )				{	itemMap.put("submittedON", f.getSubmittedON()); hasEntries = true; }
    				if ( fields.equalsIgnoreCase("modifiedON") )				{	itemMap.put("modifiedON", f.getModifiedON()); hasEntries = true; }
    				if ( fields.equalsIgnoreCase("ratedBY") )					{	itemMap.put("ratedBY", f.getRatedBY()); hasEntries = true; }
    				
    			}    			
				iROWS++;    			
    			objList.add(itemMap);
    		}
    	} catch ( Exception e ) { 
    		e.printStackTrace(); 
    	}
    	
    	return objList;	
	}
	
	public List<HashMap<String,Object>> getCompleteData() {
		List<HashMap<String, Object>> objList = new LinkedList<HashMap<String,Object>>();
		Objectify ofy = ObjectifyService.begin();
		iROWS = -1;
		
    	try {
    		Query<Rating> q = ofy.query(Rating.class);
 
    		for ( Rating f : q.fetch() ) {
    			
        			HashMap<String,Object> itemMap = new HashMap<String,Object>();
        			
    				itemMap.put("rateID", f.getRateID());
    				itemMap.put("solID", f.getSolID());
    				itemMap.put("nominatedON", f.getNominatedON());
    				itemMap.put("nominateSolutionYear", f.getNominateSolutionYear());
    				itemMap.put("nominateSolutionMonth", f.getNominateSolutionMonth());
    				itemMap.put("bestOfRegionON", f.getBestOfRegionON());
    				itemMap.put("bestOfRegionYear", f.getBestOfRegionYear());
    				itemMap.put("bestOfRegionMonth", f.getBestOfRegionMonth());
    				itemMap.put("bestOfBestON", f.getBestOfBestON());    				
    				itemMap.put("bestOfBestYear", f.getBestOfBestYear());    				
    				itemMap.put("bestOfBestMonth", f.getBestOfBestMonth());
    				itemMap.put("submittedON", f.getSubmittedON());
    				itemMap.put("modifiedON", f.getModifiedON());
    				itemMap.put("ratedBY", f.getRatedBY());
    				
    				hasEntries = true;
    				iROWS++;
    				
    				objList.add(itemMap);
    		}
    	} catch ( Exception e ) { 
    		e.printStackTrace(); 
    	}
    	
    	return objList;	
	}

	public List<HashMap<String,Object>> getQuery(String fieldName, String Value) {
		List<HashMap<String, Object>> objList = new LinkedList<HashMap<String,Object>>();
		Objectify ofy = ObjectifyService.begin();
		iROWS = -1;
		
    	try {
    		Query<Rating> q = ofy.query(Rating.class).filter(fieldName, Value);
 
    		for ( Rating f : q.fetch() ) {
    			
        			HashMap<String,Object> itemMap = new HashMap<String,Object>();
        			
    				itemMap.put("rateID", f.getRateID());
    				itemMap.put("solID", f.getSolID());
    				itemMap.put("nominatedON", f.getNominatedON());
    				itemMap.put("nominateSolutionYear", f.getNominateSolutionYear());
    				itemMap.put("nominateSolutionMonth", f.getNominateSolutionMonth());
    				itemMap.put("bestOfRegionON", f.getBestOfRegionON());
    				itemMap.put("bestOfRegionYear", f.getBestOfRegionYear());
    				itemMap.put("bestOfRegionMonth", f.getBestOfRegionMonth());
    				itemMap.put("bestOfBestON", f.getBestOfBestON());    				
    				itemMap.put("bestOfBestYear", f.getBestOfBestYear());    				
    				itemMap.put("bestOfBestMonth", f.getBestOfBestMonth());
    				itemMap.put("submittedON", f.getSubmittedON());
    				itemMap.put("modifiedON", f.getModifiedON());
    				itemMap.put("ratedBY", f.getRatedBY());
    				
    				hasEntries = true;
    				iROWS++;
    				
    				objList.add(itemMap);
    		}
    	} catch ( Exception e ) { 
    		e.printStackTrace(); 
    	}
    	
    	return objList;	
	}
	
	
	public List<HashMap<String,Object>> getQuery(String fieldName, long Value) {
		List<HashMap<String, Object>> objList = new LinkedList<HashMap<String,Object>>();
		Objectify ofy = ObjectifyService.begin();
		iROWS = -1;
		
    	try {
    		
    		Query<Rating> q = ofy.query(Rating.class).filter(fieldName, Value);
 
    		for ( Rating f : q.fetch() ) {
    			
        			HashMap<String,Object> itemMap = new HashMap<String,Object>();

    				itemMap.put("rateID", f.getRateID());
    				itemMap.put("solID", f.getSolID());
    				itemMap.put("nominatedON", f.getNominatedON());
    				itemMap.put("nominateSolutionYear", f.getNominateSolutionYear());
    				itemMap.put("nominateSolutionMonth", f.getNominateSolutionMonth());
    				itemMap.put("bestOfRegionON", f.getBestOfRegionON());
    				itemMap.put("bestOfRegionYear", f.getBestOfRegionYear());
    				itemMap.put("bestOfRegionMonth", f.getBestOfRegionMonth());
    				itemMap.put("bestOfBestON", f.getBestOfBestON());    				
    				itemMap.put("bestOfBestYear", f.getBestOfBestYear());    				
    				itemMap.put("bestOfBestMonth", f.getBestOfBestMonth());
    				itemMap.put("submittedON", f.getSubmittedON());
    				itemMap.put("modifiedON", f.getModifiedON());
    				itemMap.put("ratedBY", f.getRatedBY());
    				
    				hasEntries = true;
    				iROWS++;
    				
    				objList.add(itemMap);
    		}
    	} catch ( Exception e ) { 
    		e.printStackTrace(); 
    	}
    	
    	return objList;	
	}
	
	public int getRows(){
		return iROWS;
	}
	
}